// by Qyon 2008
// License: beerware
// email: qyonek@gmail.com
// version: 0.2
// TODO: a lot...

// USE: <a href="big_version.jpg" class="expandable"><img src="small_version.jpg" /></a>


$(document).ready(function(){
  $.expandHelper = $('<div id="expand_id"><img id="expand_img" src="" /></div>').hide();
  $('img', $.expandHelper).css({position: "absolute"});
  $('.expandable').mouseover(onCursorIn).each(
    function(i)
    {
      var preloadimg = new Image;
      preloadimg.src = this.href;
    });
  
  $.expandHelper.mouseout(onCursorOut);
  $('body').append($.expandHelper);
  
  
});

function num(el, prop) {
	return parseInt($.curCSS(el.jquery?el[0]:el,prop,true))||0;
};

function onCursorIn(eventObject) {
  var img = $('img', this);
//console.log(eventObject.clientX);
  var eh = $.expandHelper.get(0);
  eh.style.position = 'absolute';
  eh.style.left = img.offset().left;
  eh.style.top = img.offset().top;
  $.expandHelper.height(img.height()).width(img.width());
  $('img', $.expandHelper).stop();
  
  var big_img = this.href;
  $('img', $.expandHelper).get(0).src = big_img;
  if (!$('img', $.expandHelper).get(0).complete)
  {
    return;
  }
  
  var ih = new Image();
  ih.src =  big_img;
  
  $('img', $.expandHelper).height(img.height()).width(img.width());  
  
  $.expandHelper.show();
  var endLeft = ((-ih.width/2)+(img.width()/2));
  var endTop = ((-ih.height/2));
  //console.log(endLeft);
  //  console.log(endLeft + this.offsetLeft);  
  endLeft = endLeft + this.offsetLeft < 0 ? (- this.offsetLeft + 5) : endLeft;
 // console.log("%d %d ",(endLeft+this.offsetLeft+ih.width ) , document.body.clientWidth);
  endLeft = endLeft+ih.width+this.offsetLeft > document.body.clientWidth ? ((-ih.width+this.offsetLeft - document.body.clientWidth - 2*(this.offsetLeft - document.body.clientWidth) )) : endLeft;
  
  endTop = (endTop + ih.height + img.get(0).offsetTop - document.body.scrollTop) > document.body.clientHeight  ? endTop - ((endTop + ih.height + img.get(0).offsetTop) - document.body.clientHeight - document.body.scrollTop +5 ) : endTop;  
  endTop = (endTop + img.get(0).offsetTop  - document.body.scrollTop) < 0 ? ((- img.get(0).offsetTop  + document.body.scrollTop) + 5) : endTop;
  //$('img', $.expandHelper).animate({ width: ih.width+"px",
  //                                   height: ih.height+"px",
  //                                   left: (-ih.width/2+(img.width()/2))+"px",
  //                                   top: -ih.height/2+"px"
  //                                 }, 1500 );
  $('img', $.expandHelper).animate({ width: ih.width+"px",
                                     height: ih.height+"px",
                                     left: endLeft+"px",
                                     top: endTop+"px"
                                   }, 1500 );
}

function onCursorOut(eventObject) {
  $.expandHelper.hide();
  $('img', $.expandHelper).stop();
  $('img', $.expandHelper).css({width:"", height: "", left: "", top: ""});
}